package com.hello.client;

import javax.naming.Context;
import javax.naming.NamingException;

import com.hello.business.HelloWorldRemote;
import com.hello.businesslogic.HelloWorldBean;
import com.hello.clientutility.ClientUtility;

public class EJBApplicationClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HelloWorldRemote bean = doLookup();
		System.out.print(bean.sayHello());
		
	}
	
	private static HelloWorldRemote doLookup() {
		Context context = null;
		HelloWorldRemote bean = null;
		try {
			context = ClientUtility.getInitialContext();
			String lookupName = getLookupName();
			bean = (HelloWorldRemote) context.lookup(lookupName);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return bean;
	}
	
	private static String getLookupName() {
		String appName = "";
		String moduleName = "HelloWorldSessionBean";
		String distinctName = "";
		String beanName = HelloWorldBean.class.getSimpleName();
		final String interfacename = HelloWorldRemote.class.getName();
		
		return new StringBuilder("ejb:")
						.append(appName)
						.append("/")
						.append(moduleName)
						.append("/")
						.append(distinctName)
						.append("/")
						.append(beanName)
						.append("!")
						.append(interfacename)
						.toString();
	}

}
